home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfilter2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.2 KB  |  162 lines

  1. /* Copyright (C) 1991, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfilter2.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Additional filter creation */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"
  25. #include "ialloc.h"
  26. #include "idict.h"
  27. #include "idparam.h"
  28. #include "store.h"
  29. #include "strimpl.h"
  30. #include "sfilter.h"
  31. #include "scfx.h"
  32. #include "slzwx.h"
  33. #include "spdiffx.h"
  34. #include "spngpx.h"
  35. #include "ifilter.h"
  36. #include "ifilter2.h"
  37. #include "ifwpred.h"
  38.  
  39. /* ------ CCITTFaxEncode filter ------ */
  40.  
  41. /* <target> <dict> CCITTFaxEncode/filter <file> */
  42. private int
  43. zCFE(i_ctx_t *i_ctx_p)
  44. {
  45.     os_ptr op = osp;
  46.     stream_CFE_state cfs;
  47.     int code;
  48.  
  49.     check_type(*op, t_dictionary);
  50.     check_dict_read(*op);
  51.     code = zcf_setup(op, (stream_CF_state *)&cfs, iimemory);
  52.     if (code < 0)
  53.     return code;
  54.     return filter_write(i_ctx_p, 0, &s_CFE_template, (stream_state *)&cfs, 0);
  55. }
  56.  
  57. /* ------ Common setup for possibly pixel-oriented encoding filters ------ */
  58.  
  59. int
  60. filter_write_predictor(i_ctx_t *i_ctx_p, int npop,
  61.                const stream_template * template, stream_state * st)
  62. {
  63.     os_ptr op = osp;
  64.     int predictor, code;
  65.     stream_PDiff_state pds;
  66.     stream_PNGP_state pps;
  67.  
  68.     if (r_has_type(op, t_dictionary)) {
  69.     if ((code = dict_int_param(op, "Predictor", 0, 15, 1, &predictor)) < 0)
  70.         return code;
  71.     switch (predictor) {
  72.         case 0:        /* identity */
  73.         predictor = 1;
  74.         case 1:        /* identity */
  75.         break;
  76.         case 2:        /* componentwise horizontal differencing */
  77.         code = zpd_setup(op, &pds);
  78.         break;
  79.         case 10:
  80.         case 11:
  81.         case 12:
  82.         case 13:
  83.         case 14:
  84.         case 15:
  85.         /* PNG prediction */
  86.         code = zpp_setup(op, &pps);
  87.         break;
  88.         default:
  89.         return_error(e_rangecheck);
  90.     }
  91.     if (code < 0)
  92.         return code;
  93.     } else
  94.     predictor = 1;
  95.     if (predictor == 1)
  96.     return filter_write(i_ctx_p, npop, template, st, 0);
  97.     {
  98.     /* We need to cascade filters. */
  99.     ref rtarget, rdict;
  100.     int code;
  101.  
  102.     /* Save the operands, just in case. */
  103.     ref_assign(&rtarget, op - 1);
  104.     ref_assign(&rdict, op);
  105.     code = filter_write(i_ctx_p, npop, template, st, 0);
  106.     if (code < 0)
  107.         return code;
  108.     /* filter_write changed osp.... */
  109.     op = osp;
  110.     code =
  111.         (predictor == 2 ?
  112.          filter_write(i_ctx_p, 0, &s_PDiffE_template, (stream_state *)&pds, 0) :
  113.          filter_write(i_ctx_p, 0, &s_PNGPE_template, (stream_state *)&pps, 0));
  114.     if (code < 0) {
  115.         /* Restore the operands.  Don't bother trying to clean up */
  116.         /* the first stream. */
  117.         osp = ++op;
  118.         ref_assign(op - 1, &rtarget);
  119.         ref_assign(op, &rdict);
  120.         return code;
  121.     }
  122.     /*
  123.      * Mark the compression stream as temporary, and propagate
  124.      * CloseTarget from it to the predictor stream.
  125.      */
  126.     filter_mark_strm_temp(op, 2);
  127.     return code;
  128.     }
  129. }
  130.  
  131. /* ------ LZW encoding filter ------ */
  132.  
  133. /* <target> LZWEncode/filter <file> */
  134. /* <target> <dict> LZWEncode/filter <file> */
  135. /*
  136.  * Note: the default implementation of this filter, in slzwce.c,
  137.  * does not use any algorithms that could reasonably be claimed
  138.  * to be subject to Unisys' Welch Patent.
  139.  */
  140. private int
  141. zLZWE(i_ctx_t *i_ctx_p)
  142. {
  143.     os_ptr op = osp;
  144.     stream_LZW_state lzs;
  145.     int code = zlz_setup(op, &lzs);
  146.  
  147.     if (code < 0)
  148.     return code;
  149.     return filter_write_predictor(i_ctx_p, 0, &s_LZWE_template,
  150.                   (stream_state *) & lzs);
  151. }
  152.  
  153. /* ================ Initialization procedure ================ */
  154.  
  155. const op_def zfilter2_op_defs[] =
  156. {
  157.     op_def_begin_filter(),
  158.     {"2CCITTFaxEncode", zCFE},
  159.     {"1LZWEncode", zLZWE},
  160.     op_def_end(0)
  161. };
  162.